package online.sixfish.doubao.controller;

import com.vdurmont.emoji.EmojiParser;
import online.sixfish.common.api.ApiResult;
import online.sixfish.common.dto.PageDTO;
import online.sixfish.common.pojo.User;
import online.sixfish.doubao.event.ArticleViewEvent;
import online.sixfish.doubao.model.dto.AddArticleDTO;
import online.sixfish.doubao.model.entity.Article;
import online.sixfish.doubao.model.vo.ArticleVO;
import online.sixfish.doubao.service.ArticleService;
import online.sixfish.doubao.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    ArticleService articleService;
    @Resource
    UserService userService;
    @Resource
    ApplicationEventPublisher eventPublisher;

    @GetMapping("/search")
    public ApiResult search(@RequestParam(required = false) String keyword,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageDTO dto = articleService.search(keyword, pageNum, pageSize);
        return ApiResult.success(dto);
    }

    @GetMapping("/list")
    public ApiResult list(@RequestParam(value = "type", defaultValue = "1") Integer type,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageDTO<Article> dto = articleService.list(type, pageNum, pageSize);
        List<ArticleVO> list = dto.getData().stream().map(article -> {
            User user = userService.findById(article.getUserId());
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article, articleVO);
            articleVO.setAlias(user.getAlias());
            articleVO.setAvatar(user.getAvatar());
            articleVO.setUsername(user.getUsername());
            return articleVO;
        }).collect(Collectors.toList());
        PageDTO<ArticleVO> pageDTO = new PageDTO<>();
        pageDTO.setData(list);
        pageDTO.setTotal(dto.getTotal());
        pageDTO.setCurrentPageNum(dto.getCurrentPageNum());
        pageDTO.setCurrentPageCount(dto.getCurrentPageCount());
        return ApiResult.success(pageDTO);
    }

    @PostMapping("/add")
    @PreAuthorize("hasRole('user')")
    public ApiResult addArticle(@RequestBody AddArticleDTO addArticleDTO) {
        addArticleDTO.setContent(EmojiParser.parseToAliases(addArticleDTO.getContent()));
        String id = articleService.add(addArticleDTO);
        return ApiResult.success(id);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('user','admin')")
    public ApiResult viewArticle(@PathVariable String id) {
        Article article = articleService.getById(id);
        article.setContent(EmojiParser.parseToUnicode(article.getContent()));
        eventPublisher.publishEvent(new ArticleViewEvent(this, article.getId()));
        return ApiResult.success(article);
    }

    @PreAuthorize("hasAnyRole('user')")
    @GetMapping("/recommend")
    public ApiResult<List<Article>> getRecommend(@RequestParam("topicId") String id) {
        List<Article> topics = articleService.getRecommend(id);
        return ApiResult.success(topics);
    }

    @PreAuthorize("hasAnyRole('user')")
    @GetMapping("/delete")
    public ApiResult delete(@RequestParam("id") String id) {
        articleService.deleteArticle(id);
        return ApiResult.success();
    }


}
